package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.mapper.ResumeProjectExpMapper;
import com.zhilei.pojo.ResumeProjectExp;
import com.zhilei.pojo.bo.EditProjectExpBO;
import com.zhilei.service.ResumeProjectExpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class ResumeProjectExpServiceImpl extends BaseInfoProperties implements ResumeProjectExpService {

    @Autowired
    private ResumeProjectExpMapper resumeProjectExpMapper;

    /**
     * 查询项目经验
     *
     * @param projectExpId 项目表的id
     * @param userId       用户id
     * @return
     */
    @Override
    public ResumeProjectExp getProjectExp(String projectExpId, String userId) {
        ResumeProjectExp resumeProjectExp = resumeProjectExpMapper.selectOne(new QueryWrapper<ResumeProjectExp>()
                .eq("id", projectExpId)
                .eq("user_id", userId));
        return resumeProjectExp;
    }

    /**
     * 保存、编辑项目经验
     *
     * @param editProjectExpBO 保存项目经验bo类
     * @return
     */
    @Transactional
    @Override
    public void editProjectExp(EditProjectExpBO editProjectExpBO) {
        String id = editProjectExpBO.getId();
        String userId = editProjectExpBO.getUserId();
        String resumeId = editProjectExpBO.getResumeId();

        ResumeProjectExp resumeProjectExp = new ResumeProjectExp();
        BeanUtils.copyProperties(editProjectExpBO, resumeProjectExp);

        if (StringUtils.isBlank(id)) {
            // 新增操作
            resumeProjectExp.setCreateTime(LocalDateTime.now());
            resumeProjectExp.setUpdatedTime(LocalDateTime.now());
            resumeProjectExpMapper.insert(resumeProjectExp);
        } else {
            // 编辑操作
            resumeProjectExp.setUpdatedTime(LocalDateTime.now());
            resumeProjectExpMapper.update(resumeProjectExp, new QueryWrapper<ResumeProjectExp>()
                    .eq("id", id)
                    .eq("user_id", userId)
                    .eq("resume_id", resumeId));
        }

        redis.del(REDIS_RESUME_INFO + ":" + userId);
    }

    /**
     * 删除项目经验
     *
     * @param projectExpId 经验表的id
     * @param userId       用户表的id
     * @return
     */
    @Transactional
    @Override
    public void deleteProjectExp(String projectExpId, String userId) {
        resumeProjectExpMapper.delete(new QueryWrapper<ResumeProjectExp>()
                .eq("id", projectExpId)
                .eq("user_id", userId));
        redis.del(REDIS_RESUME_INFO + ":" + userId);
    }


}



































